﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Jy123.BLL.Public;
using Jy123.Model;

namespace Jy123.BLL.User
{
    public class Gift
    {
        /// <summary>
        /// 后台礼物列表
        /// </summary>
        /// <param name="count">需要获取的数量</param>
        /// <param name="strWhere">筛选条件</param>
        /// <returns>数据集</returns>
        //public DataSet ManageGiftList(int count,string strWhere)
        //{
        //    StringBuilder strSql = new StringBuilder();
        //    strSql.Append("select TOP(@Count) * from V_Manage_GiftList ");
            
        //    if (strWhere.Trim() != "")
        //    {
        //        strSql.Append("where " + strWhere);
        //    }
        //    strSql.Append(" order by ID desc ");
        //    SqlParameter[] parameters = {
        //                                new SqlParameter("@Count", SqlDbType.Int, 4)};
        //    parameters[0].Value = count;
        //    return DBUtility.DbHelperSQL.Query(strSql.ToString(), parameters);
        //}

        /// <summary>
        /// 后台礼物列表
        /// </summary>
        /// <param name="cpage">当前页</param>
        /// <param name="psize">需要获取的数量</param>
        /// <param name="strWhere">筛选条件</param>
        /// <param name="counts">总数[0],页数[1]</param>
        /// <returns>数据集</returns>
        public DataSet ManageGiftList(int cpage, int psize, string strWhere, out List<int?> counts)
        {
            return Pagination.GetList(new MPagination()
            {
                Filter = (string.IsNullOrEmpty(strWhere)) ? null : strWhere,
                CurrentPage = cpage,
                Fields = "*",
                PageSize = psize,
                PrimaryKey = "ID",
                Tables = "V_Manage_GiftList",
                Sort = "ID desc"
            }, out counts);
        }
        /// <summary>
        /// 前台礼物列表
        /// </summary>
        /// <param name="cpage">当前页</param>
        /// <param name="psize">需要获取的数量</param>
        /// <param name="strWhere">筛选条件</param>
        /// <param name="counts">总数[0],页数[1]</param>
        /// <returns>数据集</returns>
        public DataSet GiftList(int cpage, int psize, string strWhere, out List<int?> counts)
        {
            return Pagination.GetList(new MPagination()
            {
                Filter = (string.IsNullOrEmpty(strWhere)) ? null : strWhere,
                CurrentPage = cpage,
                Fields = "ID,GName,Picture,FGiftClass",
                PageSize = psize,
                PrimaryKey = "ID",
                Tables = "Public_Gift",
                Sort = "ID desc"
            }, out counts);
        }
        /// <summary>
        /// 赠送礼物记录列表
        /// </summary>
        /// <param name="cpage">当前页</param>
        /// <param name="psize">需要获取的数量</param>
        /// <param name="strWhere">筛选条件</param>
        /// <returns>数据集</returns>
        public DataSet GiftRecord(int cpage, int psize, string strWhere, out List<int?> counts)
        {
            return Pagination.GetList(new MPagination()
            {
                Filter = (string.IsNullOrEmpty(strWhere)) ? null : strWhere,
                CurrentPage = cpage,
                Fields = "*",
                PageSize = psize,
                PrimaryKey = "ID",
                Tables = "V_User_GiftRecord",
                Sort = "ID desc"
            },out counts);
        }
        /// <summary>
        /// 获取礼物分类及对应分类的礼物数量
        /// </summary>
        /// <param name="cpage">当前页</param>
        /// <param name="psize">需要获取的数量</param>
        /// <param name="strWhere">筛选条件</param>
        /// <returns>数据集</returns>
        public DataSet GiftClassCount(int cpage, int psize, string strWhere, string strSort, out List<int?> counts)
        {
            return Pagination.GetList(new MPagination()
                {
                    Tables = "V_User_GiftClassCount",
                    PrimaryKey = "ID",
                    Fields = "*",
                    CurrentPage = cpage,
                    PageSize = psize,
                    Filter = (string.IsNullOrEmpty(strWhere)) ? null : strWhere,
                    Sort = strSort
                }, out counts);
        }
        public DataSet GiftClassCount(string strWhere)
        {
            StringBuilder sbSql = new StringBuilder("select * from V_User_GiftClassCount");
            sbSql.Append(" where @strWhere");
            SqlParameter[] parameter ={
                                      new SqlParameter("@strWhere",SqlDbType.VarChar,200)};
            parameter[0].Value = strWhere;
            return DBUtility.DbHelperSQL.Query(sbSql.ToString(),parameter);
        }
        public DataSet GiftClassCount()
        {
            StringBuilder sbSql = new StringBuilder("select * from V_User_GiftClassCount order by id desc");
            //sbSql.Append(" where @strWhere");
            //SqlParameter[] parameter ={
            //                          new SqlParameter("@strWhere",SqlDbType.VarChar,200)};
            //parameter[0].Value = strWhere;
            return DBUtility.DbHelperSQL.Query(sbSql.ToString());
        }

        /// <summary>
        /// 统计用户收到的礼物数量
        /// </summary>
        /// <param name="RecieverID">收礼物的用户ID </param>
        /// <returns></returns>
        public string CountReceiveGift(int RecieverID)
        {
            string strSql = "select count(ID) from dbo.V_User_GiftRecord where Freciever = @RecieverID";
            SqlParameter[] parameter ={
                                      new SqlParameter("@RecieverID",SqlDbType.Int)};
            parameter[0].Value = RecieverID;
            string count = "0";           
            count = DBUtility.DbHelperSQL.GetSingle(strSql, parameter).ToString();
            return count;
        }

        /// <summary>
        /// 好友收到的礼物记录
        /// </summary>
        /// <param name="cpage">当前页</param>
        /// <param name="psize">需要获取的数量</param>
        /// <param name="strWhere">筛选条件</param>
        /// <returns>数据集</returns>
        public DataSet GiftRecordFriendTop1(int userID, int cpage, int psize, string strWhere)
        {
            return Pagination.GetList(new MPagination()
            {
                Filter = "(FReciever IN (SELECT FFriend FROM User_Friend WHERE (State = 1) AND (FUser = '" + userID + "')))",
                CurrentPage = cpage,
                Fields = "*",
                PageSize = psize,
                PrimaryKey = "ID",
                Tables = "V_User_GiftRecord_Friend_Top1",
                Sort = "CreatTime DESC"
            });
        }
    }
}
